package com.code2roc.xhood.util;

import org.apache.commons.lang3.StringUtils;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.*;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class CommonUtil {
    public static String encryptByMD5(String str) {
        String md5_32 = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(str.getBytes("UTF-8"));
            byte b[] = md5.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int j = 0; j < b.length; j++) {
                i = b[j];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            md5_32 = buf.toString();
        } catch (Exception e) {

        }
        return md5_32;
    }

    public static String encryptByDES(String data, String key) {
        String strs = "";
        try {
            SecureRandom sr = new SecureRandom();
            DESKeySpec dks = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
            byte[] bt = cipher.doFinal(data.getBytes("UTF-8"));
            strs = Base64.getEncoder().encodeToString(bt);
        } catch (Exception e) {

        }
        return strs;
    }

    private static final String CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String generateRandomString() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(8);

        for (int i = 0; i < 8; i++) {
            int index = random.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(index);
            sb.append(randomChar);
        }

        return sb.toString();
    }

    public static String getExecuteUnitInfo(Date startDate) {
        Date endDate = new Date();
        long unit = (endDate.getTime() - startDate.getTime());
        return unit + "ms";
    }

    public static String replace(String content, String oldChar, String newChar) {
        if(StringUtils.isNotEmpty(content) && StringUtils.isNotEmpty(oldChar) && StringUtils.isNotEmpty(oldChar)){
            StringBuilder sb = new StringBuilder(content);
            int index = 0;
            while((index = sb.indexOf(oldChar, index)) != -1) {
                sb.replace(index, index + oldChar.length(), newChar);
                index += newChar.length();
            }
            return sb.toString();
        }else{
            return content;
        }
    }
}
